import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 加载数据
data = pd.read_csv("C:\\Users\\董柳蕙\\Downloads\\heart.csv")

# 计算目标变量与数值变量之间的相关性
numerical_columns = ['age', 'trestbps', 'chol', 'thalach', 'oldpeak', 'ca', 'thal']  # 替换为您的数值变量
target_correlation = data[numerical_columns + ['target']].corr()['target'][:-1]

# 绘制相关性热力图
plt.figure(figsize=(8, 6))
sns.heatmap(
    data[numerical_columns + ['target']].corr(),
    annot=True,
    cmap='coolwarm',
    fmt='.2f',
    cbar=True,
    linewidths=0.5
)
plt.title("数值变量与目标变量相关性热力图", fontsize=16)
plt.tight_layout()
plt.show()

# 输出与目标变量的相关系数
print("目标变量与数值变量的相关性：")
print(target_correlation)
